home *** CD-ROM | disk | FTP | other *** search
/ World of Amiga / World of Amiga.iso / archive / compression / lister12.lha / ListTar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-01  |  2.3 KB  |  91 lines

  1. /* List a Tar archive */
  2. BOOL
  3. ListTar(ULONG type,UBYTE *infile)
  4. {
  5.   ULONG tfiles = 0, tcomp = 0, tuncomp = 0, time, uncomp;
  6.   FILE *fp;
  7.   struct TarInfoBlock tar;
  8.   struct tm *tm;
  9.   UBYTE time_str[25];
  10.  
  11.   /* Open the file up.  Abort if file not found or error occurs */
  12.   fp = OpenFile(type,infile);
  13.  
  14.   if (!GetHeader(&tar,fp) || !Checksum(&tar)) return WRONG_ARCHIVE;
  15.   fseek(fp,0,SEEK_SET);
  16.  
  17.   /* Initialize the start of the list */
  18.   InitList(TAR,infile);
  19.  
  20.   /* Loop until there is a Zero in the first byte of the name */
  21.   while (GetHeader(&tar,fp))
  22.     {
  23.       if (!Checksum(&tar))
  24.         {
  25.           printf("\nChecksum Error in File Header - Aborting\n");
  26.           break;
  27.         }
  28.  
  29.       /* Read in the uncompressed size */
  30.       sscanf (tar.size,"%8o\n",&uncomp);
  31.  
  32.       /* Read in date of stored file and put it in string */
  33.       sscanf (tar.mtime,"%12o",&time);
  34.       tm = localtime (&time);
  35.  
  36.       sprintf (time_str,"%02d-%s-19%d %02d:%02d:%02d",
  37.         tm->tm_mday,months[tm->tm_mon],tm->tm_year,tm->tm_hour,
  38.         tm->tm_min,tm->tm_sec);
  39.  
  40.       /* Print out statistics read in */
  41.       PrintList(tar.name, uncomp, uncomp,&tfiles,&tcomp,&tuncomp,time_str);
  42.  
  43.       /* Seek to next header in list */
  44.       fseek (fp,512 - (sizeof tar) + uncomp,SEEK_CUR);
  45.       if ( (uncomp != 0) && ((uncomp % 512) != 0) )
  46.        {
  47.          uncomp = 512 - uncomp % 512;
  48.          fseek (fp,uncomp,SEEK_CUR);
  49.        }
  50.     }
  51.  
  52.    /* Close the file and print the ending statistics. */
  53.    fclose (fp);
  54.    EndStats(tfiles,tcomp,tuncomp);
  55.    return (TRUE);
  56. }
  57.  
  58. /* Function to read the tar header structure into memory */
  59. BOOL
  60. GetHeader(struct TarInfoBlock *tar,FILE *fp)
  61. {
  62.   fread (tar->name,1,100,fp);
  63.   if (tar->name[0] == 0)
  64.     return (FALSE);
  65.   fread (tar->mode,1,8,fp);
  66.   fread (tar->uid,1,8,fp);
  67.   fread (tar->gid,1,8,fp);
  68.   fread (tar->size,1,12,fp);
  69.   fread (tar->mtime,1,12,fp);
  70.   fread (tar->chksum,1,8,fp);
  71.   fread (tar->linkflag,1,1,fp);
  72.   fread (tar->linkname,1,100,fp);
  73.   fread (tar->rdev,1,6,fp);
  74.   return (TRUE);
  75. }
  76.  
  77. BOOL
  78. Checksum (struct TarInfoBlock *tar)
  79. {
  80.   ULONG checksum = 0,ok;
  81.   char *i;
  82.  
  83.   for (i = tar->name; i <= tar->mtime+11;i++)
  84.     checksum += *i;
  85.   checksum += 8*32;
  86.   for (i = tar->linkflag; i <= tar->rdev+5;i++)
  87.     checksum += *i;
  88.   sscanf(tar->chksum,"%8o",&ok);
  89.   return (ok == checksum);
  90. }
  91.